package com.weipu.common.facade.impl;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.util.Log;

import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.CreditCardServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.AddressModel;
import com.weipu.common.facade.model.CreditCardInfo;
import com.weipu.common.facade.model.CreditCardModel;
import com.weipu.common.facade.model.CreditCardPayRespose;
import com.weipu.common.facade.model.CreditCardReqParams;

public class CreditCardServiceProviderImpl extends BaseServiceProviderImpl
		implements CreditCardServiceProvider {
	private static final String TAG = CreditCardServiceProviderImpl.class
			.getName();

	private static Map<String, String> mapUrl;

	// private static Map<String, HttpMethod> mapMethod;

	// 初始化方法名和url键和值存放
	static {
		mapUrl = new HashMap<String, String>();
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_GETCREDITCARDINFOS,
				Constant.GETCREDITCARDINFOS_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_DOCREDITCARDCHECKOUT,
				Constant.DOCREDITCARDCHECKOUT_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_GETCREDITTYPE,
				Constant.GETCREDITTYPE_URL);
		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_DELETECREDITCARD,
				Constant.DELETECREDITCARD_URL);

		mapUrl.put(
				ServerConstant.RequestMethodNameConstant.METHOD_DELETECREDITCARD,
				Constant.DELETECREDITCARD_URL);
	}

	@Override
	public CreditCardInfo getCreditCardInfos(long customerId, String token,
			boolean isNormal, int pageIndex, int pageSize) throws DxException {
		CreditCardReqParams params = new CreditCardReqParams();
		Log.i("Ttt", customerId + " " + token + " " + isNormal + " "
				+ pageIndex + " " + pageSize);
		params.setCustomerID(customerId);
		params.setToken(token);
		params.setIsNormal(true);
		params.setPageIndex(pageIndex);
		params.setPageSize(pageSize);
		JSONObject data = requestHttpPostData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCREDITCARDINFOS),
				params);
		return CreditCardInfo.jsonCreditCardInfoModel(data);
	}

	@Override
	public String getCreditType(long customerId, String token,
			String cardNumber, boolean IsSecPay) throws DxException {
		CreditCardReqParams params = new CreditCardReqParams();
		params.setCustomerID(customerId);
		params.setToken(token);
		params.setCardNumber(cardNumber);
		params.setIsSecPay(false);
		JSONObject jsonObject = requestHttpPostAll(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCREDITTYPE),
				params);
		if (null != jsonObject) {
			return jsonObject
					.optString(ServerConstant.CreditCard.CREDITCARD_CARDTYPE);
		}
		return null;
	}

	@Override
	public CreditCardPayRespose doCreditCardCheckout(String cicid,
			String token, String orderNumber,
			AddressModel.Address billingAddress, boolean usePastCard,
			boolean isRememberCard, CreditCardModel creditCard,
			CreditCardModel creditCardToken, String email) throws DxException {
		CreditCardPayRespose creditCardPayRespose = null;
		CreditCardReqParams params = new CreditCardReqParams();
		params.setCICID(cicid);
		params.setToken(token);
		params.setOrderNumber(orderNumber);
		params.setBillingAddress(billingAddress);
		params.setCreditCard(creditCard);
		params.setPaymentAction("Purchase");
		params.setUsePastCard(usePastCard);
		params.setIsRememberCard(isRememberCard);
		params.setCreditCardToken(creditCardToken);
		params.setEmail(email);
		JSONObject data = requestHttpPostData(
				mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_DOCREDITCARDCHECKOUT),
				params);
		creditCardPayRespose = new CreditCardPayRespose();
		creditCardPayRespose
				.setOrderNumber(data
						.optString(ServerConstant.Parameter.PARAMETER_NAME_ORDER_NUMBER));
		creditCardPayRespose
				.setResponseResult(data
						.optInt(ServerConstant.Parameter.PARAMETER_NAME_RESPONSERESULT));
		creditCardPayRespose
				.setRiskControlStatus(data
						.optInt(ServerConstant.Parameter.PARAMETER_NAME_RISKCONTROLSTATUS));
		creditCardPayRespose.setOrderTotal(data
				.optDouble(ServerConstant.Parameter.PARAMETER_NAME_ORDERTOTAL));
		creditCardPayRespose.setCurrencyCode(data
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE));
		creditCardPayRespose.setCurrencyCodeSymbol(data
				.optString(ServerConstant.Product.CONSTANT_CURRENCYCODESYMBOL));
		return creditCardPayRespose;
	}

	@Override
	public void deleteCreditCard(String creditCardID, int customerID,
			String token) throws DxException {
		CreditCardReqParams params = new CreditCardReqParams();
		params.setCreditCardID(creditCardID);
		params.setCustomerID(customerID);
		params.setToken(token);
		JSONObject jsonObject =requestHttpPostAll(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_DELETECREDITCARD), params);
	}

}
